package com.food.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.food.user.commos.GlobleStaticNum;
import com.food.user.dto.UserAccountInfoDto;
import com.food.user.entity.UserAccount;
import com.food.user.entity.UserAccountInfo;
import com.food.user.form.UpdateUserAccountInfoForm;
import com.food.user.mapper.UserAccountInfoMapper;
import com.food.user.mapper.UserAccountMapper;
import com.food.user.service.UserAccountInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.food.user.service.UserAccountService;
import com.food.user.util.JwtUtil;
import com.food.user.util.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 帅得一
 * @since 2023年06月17日
 */
@Service
public class UserAccountInfoServiceImpl extends ServiceImpl<UserAccountInfoMapper, UserAccountInfo> implements UserAccountInfoService {
    @Autowired
    private UserAccountInfoMapper userAccountInfoMapper;
    @Autowired
    private UserAccountMapper userAccountMapper;
    @Autowired
    private UserAccountService userAccountService;
    @Override
    public UserAccountInfoDto showUserAccountInfo() {
        String accessToken = WebUtil.getRequest().getHeader("accessToken");
        Integer id = JwtUtil.getUserId(accessToken);
        UserAccountInfo userAccountInfo = userAccountInfoMapper.selectById(id);
        UserAccount userAccount = userAccountService.showMyAccount();
        UserAccountInfoDto userAccountInfoDto = BeanUtil.toBean(userAccountInfo, UserAccountInfoDto.class);
        userAccountInfoDto.setUserName(userAccount.getUserName());
        userAccountInfoDto.setUserNickName(userAccount.getUserNickName());
        userAccountInfoDto.setUserPhone(userAccount.getUserPhone());
        if(userAccountInfo.getUserInfoSex()!=null){
            if(userAccountInfo.getUserInfoSex()== GlobleStaticNum.SEX_WOMAN){//女
                userAccountInfoDto.setUserInfoSex("女");
            }else if(userAccountInfo.getUserInfoSex()==GlobleStaticNum.SEX_MAN){
                userAccountInfoDto.setUserInfoSex("男");
            }
        }
        return userAccountInfoDto;
    }

    @Override
    public void updateUserAccountInfo(UpdateUserAccountInfoForm form) {
        String accessToken = WebUtil.getRequest().getHeader("accessToken");
        Integer userId = JwtUtil.getUserId(accessToken);
        //再更新账号信息表里的信息
        UpdateWrapper<UserAccountInfo> userAccountInfoWrapper = new UpdateWrapper<>();
        userAccountInfoWrapper.eq("user_id",userId);
        userAccountInfoWrapper.set("user_info_real_name",form.getUserInfoRealName());
        userAccountInfoWrapper.set("user_info_id_card",form.getUserInfoIdCard());
        userAccountInfoWrapper.set("user_info_sex",form.getUserInfoSex());
        userAccountInfoWrapper.set("user_info_age",form.getUserInfoAge());
        userAccountInfoMapper.update(null,userAccountInfoWrapper);
    }
}
